package com.iflytek.cosmo.ocr.web.controller;


import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

import com.iflytek.cosmo.ocr.common.config.ArgusProperties;
import com.iflytek.cosmo.ocr.common.constant.CommonConstant;
import com.iflytek.cosmo.ocr.common.constant.LicenseConstant;
import com.iflytek.cosmo.ocr.common.util.InetAddressUtil;
import com.iflytek.cosmo.ocr.model.domain.AuthParam;
import com.iflytek.cosmo.ocr.model.domain.MessageOutput;
import com.iflytek.cosmo.ocr.model.domain.State;
import com.iflytek.cosmo.ocr.service.ArgusPostService;
import com.iflytek.cosmo.ocr.service.IflyBarAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api("语音助手授权模块")
@RestController
@RequestMapping(value = "/auth")
public class IflyBarAuthController {

    /**
     * 日志记录器
     */
    private Logger logger = LoggerFactory.getLogger(IflyBarAuthController.class);

    /**
     * 授权service
     */
    @Autowired
    private IflyBarAuthService iflyBarAuthService;

    /**
     * 数据分析平台接口调用service
     */
    @Autowired
    private ArgusPostService argusPostService;
    
    @Autowired
    ArgusProperties argusProperties;

    /**
     * 授权验证
     *
     * @param param 授权验证入参
     * @return 授权验证是否成功
     */
    @ApiOperation(value = "授权验证", notes = "授权验证", httpMethod = "POST")
    @PostMapping(value = "/verify")
    public MessageOutput<Boolean> verifyAuth(
            @ApiParam(value = "授权入参对象", required = true) @Valid @RequestBody AuthParam param,
            HttpServletRequest request) {
        // 用户权限校验
        Boolean auth = iflyBarAuthService.verifyAuth(param.getMac(), InetAddressUtil.getRemoteIpAddr(request));
        // 输出封装
        if (!auth) {
            logger.info(LicenseConstant.ANTHORIZATION_FAILURES);
            return new MessageOutput<Boolean>(
                    new State(CommonConstant.FAIL_CODE, LicenseConstant.ANTHORIZATION_FAILURES), auth);
        }
        //启动埋点
        if (argusProperties.getBuryingEnable()) {
        	argusPostService.postAgent(InetAddressUtil.getRemoteIpAddr(request), argusProperties.getOcrStartUpNum(), 1, "macAd=" + param.getMac()[0], null);
        }
        return new MessageOutput<Boolean>(new State(CommonConstant.SUCCESS_CODE, LicenseConstant.ANTHORIZATION_SUCCESS),
                auth);
    }

    /**
     * mac地址与授权码绑定
     *
     * @param param mac地址与授权码绑定入参
     * @return mac地址与授权码绑定成功与否
     */
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "mac地址与授权码绑定", notes = "mac地址与授权码绑定", httpMethod = "POST")
    @PostMapping(value = "/register")
    public MessageOutput<Object> register(
            @ApiParam(value = "授权入参对象", required = true) @Valid @RequestBody AuthParam param,
            HttpServletRequest request) {
    	/*if (argusProperties.getBuryingEnable()) {
        	argusPostService.postAgent(InetAddressUtil.getRemoteIpAddr(request), argusProperties.getOcrInstallNum(), 1, "macAd=" + param.getMac()[0], null);
        }*/
        return iflyBarAuthService.register(param.getMac(), param.getCode());
    }

    /**
     * 生成授权码
     *
     * @param num 生成授权码数量
     * @return 授权码
     */
	// @ApiOperation(value = "生成授权码", notes = "生成授权码", httpMethod = "GET")
	// @GetMapping(value = "/license/{num}")
	public List<String> creatLicenseCode(@ApiParam(value = "生成授权码个数") @PathVariable("num") Integer num) {
		return iflyBarAuthService.createLicenseCode(num);
	}

    /**
     * 查询已创建还没使用的授权码
     *
     * @return 还没使用的授权码
     */
    // @ApiOperation(value = "查询已创建还没使用的授权码", notes = "查询已创建还没使用的授权码", httpMethod = "GET")
    // @GetMapping(value = "/license")
    public List<String> getLicenseCode() {
        return iflyBarAuthService.getValidLicenseCode();
    }

}
